import request from '@/utils/request'
/********************************************************************
 created:	2017/05/26
 created:	16:10:2014   09:58
 filename:
 file path:
 file base:
 file ext:
 author:		chenyadong

 purpose:	CA 控件、插件js统一脚本
 *********************************************************************/

//对于IE11的判断需要使用userAgent中的Trident进行判断
export function isIE(){

	var isIEof11 = isIE11();
	var bisIE = checkBrowserISIE();
	return ((navigator.appName.indexOf("Microsoft Internet Explorer")!=-1 && document.all) || isIEof11 || bisIE);
}

export function checkBrowserISIE()
{
	return (!!window.ActiveXObject || 'ActiveXObject' in window) ? true : false;
}

//IE11的判断已经不能使用MSIE来判断 userAgent：Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko
export function isIE11(){
	var trident = navigator.userAgent.split(";")[2];
	var trident64 = navigator.userAgent.split(";")[3];

	if(trident == null && trident64 == null)
	{
		//alert("trident is null");
		return false;
	}

	var isx86 = false;
	var isx64 = false;

	if(trident != null)
	{
		isx86 = navigator.userAgent.split(";")[2].toLowerCase().indexOf("trident")=="-1"?false:true;
	}

	if(trident64 != null)
	{
		isx64 = navigator.userAgent.split(";")[3].toLowerCase().indexOf("trident")=="-1"?false:true;
	}

	return isx86 || isx64;
}

//js中使用的插件对象
var plugin = zwPlugin;

//获取插件对象
export function zwPlugin()
{
	return document.getElementById('zwPlugin');
}

/********************http/https发送、接收信息****************************************/
var _xmlhttp;

export function GetHttpResult() {
	if (_xmlhttp.readyState == 4 && _xmlhttp.status == 200) {
		var obj = eval("(" + _xmlhttp.responseText + ")");
		return obj;
	}
	else {
		return null;
	}
}

export function AjaxIO(json_in) {
	try{
		var _url= "http://127.0.0.1:8198";
		var json = json_in;
		if (_xmlhttp == null) {
			if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
				_xmlhttp = new XMLHttpRequest();
			} else { // code for IE6, IE5
				_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}

		if ("https:" == document.location.protocol) {
			_url = "https://127.0.0.1:8199";
		}

		_xmlhttp.open("POST", _url, false);
		_xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");


		_xmlhttp.send("json=" + json);
	}
	catch(e){
		alert("证书助手功能异常，请修复或重装");
	}
}

/*****************************
 检测控件是否安装
 *******************************/
export function CheckComSetup()
{
	try{
		if(!isIE())
		{
			// 调用接口
			var request = new Object();
			request.method = "checkComSetup";
			AjaxIO(JSON.stringify(request));

			var ret = GetHttpResult();
			if (ret) {

				return (ret.resultMSG==0)?true:false;
			}
		}
		else{
			plugin().CheckComSetup();
			alert("控件已经安装");
			return true;
		}
	}
	catch(e){
		alert("控件没有安装");
		return false;
	}
}

/************************************************************************/
/*    初始化服务提供者
/*  @param  provName
/*          CSP名称
/*  @param  contName
/*          容器名称
/*  @return 成功返回true， 失败返回false                                */
/************************************************************************/
export function initProv(provName, contName)
{
	contName = contName || "CCITCont1";
	try{
		var ret;
		if (!isIE())
		{
			// 调用接口
			var request = new Object();
			request.method = "InitProvider";
			request.provName = provName;
			request.contName = contName;

			AjaxIO(JSON.stringify(request));

			ret = GetHttpResult();
			console.log("wen ukey 初始化："+JSON.stringify(ret))
			if (ret) {
				if (ret.errcode != 0) {
					alert("初始化发生错误!" + "\r\n" + "错误描述：" + ret.errMsg);
				}

				return (ret.resultMSG == 0) ? true : false;
			}
		}
		else{
			ret = plugin().InitProvider(provName, contName);
			return (ret == 0) ? true : false;
		}
	}
	catch(e){
		if (e.description != "")
		{
			 alert(e.description);
		}

		return false;
	}
}

/************************************************************************/
/*   获取随机数
/*   @param randomLen
/*          要获取的随机数长度
/*   @return 成功返回base64格式的随机数,失败返回null                    */
/************************************************************************/
export function genRandom (randomLen)
{
	try{
		var ret;
		if(!isIE())
		{
			// 调用接口
			var request = new Object();
			request.method = "genRandom";
			request.randomLen = randomLen;
			AjaxIO(JSON.stringify(request));

			ret = GetHttpResult();
			if (ret) {
				if (ret.errcode != 0) {
					alert("生成随机数发生错误!" + "\r\n" + "错误描述：" + ret.errMsg);
					return null;
				}
				else{
					return ret.resultMSG;
				}
			}
		}else{
			ret =  plugin().genRandom(randomLen);
			return ret;
		}
	}
	catch(e){

		if (e.description != "")
		{
			alert(e.description);
		}

		return null;
	}
}

/************************************************************************/
/*   对源数据使用指定的签名算法进行签名
/*   @param srcData
/*          源数据
/*   @pram AlgType
/*         签名算法，见ALG_SIGN_XXX定义
/*   @return 成功返回base64格式的签名内容,失败返回null                  */
/************************************************************************/
export function signatureWithAlg (srcData, AlgType)
{
	try{
		var signdata;

		if(!isIE())
		{
			// alert(listProvider);
			// 调用接口
			var request = new Object();
			request.method = "sign";
			request.srcData = srcData;
			request.AlgType = AlgType;
			AjaxIO(JSON.stringify(request));

			var ret = GetHttpResult();
			if (ret) {
				if (ret.errcode != 0) {
					alert("数字签名发生错误!" + "\r\n" + "错误描述：" + ret.errMsg);
					return null;
				}
				else{
					return ret.resultMSG;
				}
			}
		}
		else{
			signdata = plugin().sign(srcData, AlgType);
			return signdata;
		}

	}
	catch(e){

		if (e.description != "")
		{
			// alert(e.description);
		}

		return null;
	}
}

/************************************************************************/
/*    获取设备序列号 成功返回版本号，失败返回null                                                   */
/************************************************************************/
export function readUKSN ()
{
	var ret;

	try{
		if (!isIE())
		{
			// 调用接口
			var request = new Object();
			request.method = "readDeviceSN";

			AjaxIO(JSON.stringify(request));

			ret = GetHttpResult();
			if (ret) {
				if (ret.errcode != 0) {
					alert("获取设备序列号发生错误!" + "\r\n" + "错误描述：" + ret.errMsg);
					return null;
				}
				else{
					return ret.resultMSG;
				}

			}
		}
		else{
			ret = plugin().ReadDeviceSN();

			return ret;
		}
	}
	catch(e){

		if (e.description != "")
		{
			// alert(e.description);
			this.blur();
			this.focus();
		}

		return null;
	}
}
